package com.gdsdxy.controller;

import com.gdsdxy.common.utils.RandomUtils;
import com.gdsdxy.common.vo.ViewCart;
import com.gdsdxy.domain.*;
import com.gdsdxy.service.CartService;
import com.gdsdxy.service.OrderService;
import com.gdsdxy.service.UserAddressService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService service;
    @Autowired
    private UserAddressService uaService;
    @Autowired
    private CartService cartService;

    // 下单
    @RequestMapping("addOrder")
    public String add(int t, @RequestParam(value = "total") String money, int aid, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        String oid = RandomUtils.createOrderId();
        boolean res = false;
        if (t == 1) {// 直接下单
            res = service.insertDirect(user.getId(), oid, aid, (CartDetail) session.getAttribute("direct"));
        } else {// 购物车下单
            res = service.save(oid, user.getId(), aid);
        }
        if (res) {
            // 下单成功
            model.addAttribute("oid", oid);
            model.addAttribute("money", money);
            return "pay2";
        } else {
            return "index";
        }
    }

    // 列表
    @RequestMapping("getAllOrder")
    public String all(Integer t, Integer aid, Model model, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {
        PageHelper.startPage(page, size);
        List<Order> orders = service.queryAll();
        PageInfo<Order> pageInfo = new PageInfo<Order>(orders);
        model.addAttribute("page", pageInfo);
        model.addAttribute("orders", orders);
        return "admin/showAllOrder";
    }

    //直接下单
    @RequestMapping("getDirectOrder")
    public String direct(Goods gs, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<ViewCart> cds = new ArrayList<>();
        ViewCart cd = new ViewCart();
        cd.setGid(gs.getId());
        cd.setMoney(gs.getPrice());
        cd.setPrice(gs.getPrice());
        cd.setNum(1);
        cd.setName(gs.getName());
        cds.add(cd);
        CartDetail detail = new CartDetail();
        detail.setGid(gs.getId());
        detail.setMoney(gs.getPrice());
        detail.setNum(1);
        session.setAttribute("direct", detail);
        model.addAttribute("cartList", cds);
        model.addAttribute("addList", uaService.queryByUid(user.getId()));
        model.addAttribute("type", 1);
        // 转发
        return "order";
    }

    // 查询用户的所有订单
    @RequestMapping("getOrderList")
    public String olist(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("orderList", service.queryByUid(user.getId()));
        return "orderList";
    }

    // 订单预览
    @RequestMapping("getOrderView")
    public String viewlist(HttpServletRequest request, Model model) {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("type", 2);
        request.setAttribute("cartList", cartService.queryCart(cart.getId()));
        request.setAttribute("addList", uaService.queryByUid(user.getId()));
        return "order";
    }

    // 列表
    @RequestMapping("getOrderDetail")
    public String list(String oid, HttpSession session, Model model) {
        model.addAttribute("od", service.queryOrderDetailById(oid));
        return "orderDetail";
    }

    //根据用户姓名和订单的支付状态查询订单（admin）
    @RequestMapping("selectOrderByNameAndFlag")
    public String selectByNameAndFlag(String username, Integer status, Model model, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {
        PageHelper.startPage(page, size);
        List<Order> orderList=service.selectByNameAndFlag(username, status);
        PageInfo<Order> pageInfo = new PageInfo<Order>(orderList);
        model.addAttribute("page", pageInfo);
        model.addAttribute("orderList", orderList);
        model.addAttribute("status",status);
        model.addAttribute("username",username);
        return "admin/showOrder";

    }

    //删除订单（user）
    @RequestMapping("userDeleteOrder")
    public String userDeleteOrder(String oid, HttpSession session, Model model) {
        service.deleteById(oid);
        return "redirect:getOrderList";
    }

    //修改订单（admin）
    @RequestMapping("sendOrder")
    public String sendOrder(String oid, Model model) {
        service.update(oid, 3);
        return "redirect:getAllOrder";
    }

    //修改状态
    @RequestMapping("changeStatus")
    public String changeStatus(String oid, Model model) {
        service.update(oid, 5);
        return "redirect:getOrderList";
    }

    @RequestMapping("payOrder")
    public String payOrder(String orderid, Model model) {
        service.update(orderid, 2);
        return "paySuccess";
    }


    //跳转到图表页面(数据是今年的)，带着每月订单数量和每月订单金额
    @RequestMapping("sendEcharts")
    public String echarts(Model model){
        //获得当前年份
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        List<Integer> num=service.queryOrderNum(year);
        List<Integer> money=service.queryOrderMoney(year);
        model.addAttribute("year",year);
        model.addAttribute("num",num);
        model.addAttribute("money",money);
        return "/admin/echarts";
    }

    //根据跳转到图表页面，带着每月订单数量和每月订单金额
    @RequestMapping("sendEchartsByYear")
    public String echartsByYear(Model model,int year){
        List<Integer> num=service.queryOrderNum(year);
        List<Integer> money=service.queryOrderMoney(year);
        model.addAttribute("year",year);
        model.addAttribute("num",num);
        model.addAttribute("money",money);
        return "/admin/echarts";
    }
}